package com.brother.realtime;

import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.LineNumberReader;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.log4j.Logger;

import com.brother.database.DataBase;
import com.brother.model.AlarmType;
import com.brother.model.Customer;
import com.brother.model.EmailSetting;
import com.brother.model.RealTimeData;
import com.brother.model.Tracker;
import com.brother.service.EmailSettingService;
import com.brother.service.TrackerService;
import com.brother.service.UserService;
import com.brother.util.CommonProperties;
import com.brother.util.ContentProperties;

public class AlarmEmailSender {
private static	Logger log = Logger.getLogger(AlarmEmailSender.class);
private static List<String> imeis = new CopyOnWriteArrayList<String>();
	
public static boolean sendMail(AlarmType alt, RealTimeData data,
		String language){
	
	log.debug("Beginning to send alarm email....");
	SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
	TrackerService srv = new TrackerService();
	Tracker tracker = srv.getTrackerByFieldName("trackerid", data.getTrackerid());
	
	if(!tracker.isMailEnabled()){
		return false;
	}
	
	UserService usrv = new UserService();
	Customer trackerUser = usrv.GetUserByUserId(tracker.getUserid());
	
	
	Customer parent = usrv.getSuperiorUser(trackerUser.getUserid()+"");
	if(parent == null){
		parent = trackerUser;
	}
	
	EmailSettingService emailSetSrv = new EmailSettingService();
	
	EmailSetting set = emailSetSrv.getEmailSetting(parent.getUserid()+"");
	
	if(!set.isMailEnabled()){
		return false;
	}
	
	String subject = "Tracker activated " + tracker.getTrackerName() + " alert!";;
	String content = "Dear Customer,<br/>" + tracker.getTrackerName() + " activated "+alt.name()+" alert!<br/> Thanks for using our service!";
	if(alt == AlarmType.Low_Power_Alarm){
		content = String.format(ContentProperties.getProperty("alarm.lower.power.mail.body", language),
				tracker.getTrackerName(), df.format(data.getTraceDateTime()), data.getSpeed());
	}else if(alt.name().indexOf("input") > -1)
	{
		content = String.format(ContentProperties.getProperty("alarm.input.mail.body", language),
				tracker.getTrackerName(), df.format(data.getTraceDateTime()), data.getSpeed());
	}else if(alt == AlarmType.Position_Move_Alarm)
	{
		content = String.format(ContentProperties.getProperty("alarm.position.move.mail.body", language),
				tracker.getTrackerName(), df.format(data.getTraceDateTime()), data.getSpeed());
	}else if(alt == AlarmType.Restore_GPS_Signal_Alarm)
	{
		content = String.format(ContentProperties.getProperty("alarm.restore.gps.signal.mail.body", language),
				tracker.getTrackerName(), df.format(data.getTraceDateTime()), data.getSpeed());
	}else if(alt == AlarmType.SIREN)
	{
		content = String.format(ContentProperties.getProperty("alarm.siren.mail.body", language),
				tracker.getTrackerName(), df.format(data.getTraceDateTime()), data.getSpeed());
	}else if(alt == AlarmType.SOS_Alarm)
	{
		content = String.format(ContentProperties.getProperty("alarm.sos.mail.body", language),
				tracker.getTrackerName(), df.format(data.getTraceDateTime()), data.getSpeed());
	}
	
	
	if(alt == AlarmType.Over_Speed_Alarm){
		subject = String.format(ContentProperties.getProperty("alarm.overspeed.mail.subject", language), tracker.getTrackerName());
		content = String.format(ContentProperties.getProperty("alarm.overspeed.mail.body", language), tracker.getTrackerName()
				, df.format(data.getTraceDateTime()), data.getSpeed());
	}
	if(alt == AlarmType.Geo_Fence_Alarm){
		subject = String.format(ContentProperties.getProperty("alarm.geo.mail.subject", language), tracker.getTrackerName());
		content = String.format(ContentProperties.getProperty("alarm.geo.mail.body", language),
				tracker.getTrackerName(), df.format(data.getTraceDateTime()), data.getSpeed());
	}
	if(alt == AlarmType.External_Power_Lost){
		subject = String.format(ContentProperties.getProperty("alarm.external.power.lost.mail.subject", language), tracker.getTrackerName());
		content = String.format(ContentProperties.getProperty("alarm.external.power.lost.mail.body", language), 
				tracker.getTrackerName(), df.format(data.getTraceDateTime()), data.getSpeed());
	}
	
	
	
	try {
		HtmlEmail  mail = new HtmlEmail ();
		mail.setAuthentication(set.getMailUsername(), set.getMailPassword());
		mail.setHostName(set.getMailHost());// smtp.126.com
		//mail.isSSL()
		mail.setSSL(set.isSslEnabled());
		
		String[] emails = null;
		if(trackerUser.getEmail().indexOf(";") > 0){
			emails = trackerUser.getEmail().split(";");
		}else{
			emails = trackerUser.getEmail().split(",");
		}
		mail.addTo(emails[0].trim());
		for(int i=1;i < emails.length; i++){
			mail.addTo(emails[i]);
		}
		mail.setFrom(set.getMailUsername());
		mail.setSubject(subject);
		mail.setContent(content+CommonProperties.getEmailFooter(), "text/html;charset=utf-8");
		mail.setSmtpPort(Integer.parseInt(set.getPort()));
		//mail.setMsg(context); // 用这个会中文乱码
		mail.send();
	} catch (EmailException e) {
		log.error("sending email, error...,tracker is "+tracker.getTrackerName(), e);
	}
	log.debug("Alarm email was sent! Tracker is "+tracker.getTrackerName());
	DataBase.closeThreadConnection();
	return true;
}


	
}
